package com.qf.dao.impl;

import com.qf.dao.GoodsTypeDao;
import com.qf.domain.GoodsType;
import com.qf.util.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author Administrator
 * @version v1.0
 * @date 2022/4/19
 */
public class GoodsTypeDaoImpl implements GoodsTypeDao {
    private QueryRunner queryRunner = new QueryRunner(DataSourceUtils.getDatasource());
    @Override
    public List<GoodsType> findTypeListByLevel(int level) {
        String sql="select id,name , level, parent from tb_goods_type where level = ?";
        try {
            return  queryRunner.query(sql, new BeanListHandler<>(GoodsType.class),level);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public GoodsType findById(int id) {
        String sql = "select id,name,level,Parent from tb_goods_type where id = ?";
        try {
            GoodsType goodsType  = queryRunner.query(sql , new BeanHandler<>(GoodsType.class), id);
            return goodsType;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public List<GoodsType> selectALL() {
        String sql="select id,name , level, parent from tb_goods_type ";
        try {
            return  queryRunner.query(sql, new BeanListHandler<>(GoodsType.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void update(GoodsType goodsType) {
        String sql = "update tb_goods_type set name = ?,level = ?,Parent = ?  where id = ?";
        try {
            queryRunner.update(sql,goodsType.getName(),goodsType.getLevel(),goodsType.getParent(),goodsType.getId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String selectColum(String goodsParent) {
        String sql = "select id  from tb_goods_type where name = ? ";
        try {
            String s = (queryRunner.query(sql, new ColumnListHandler(), goodsParent)).toString();
            System.out.println(s);
            return  s;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void insert(GoodsType goodsType) {
        String sql = "insert into tb_goods_type(id,name,level,Parent) values (?,?,?,?)";
        try {
            queryRunner.update(sql,goodsType.getId(),goodsType.getName(),goodsType.getLevel(),goodsType.getParent());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public long selectCount() {
        try {
            return (long)queryRunner.query(" SELECT COUNT(0) FROM tb_goods_type  ", new ScalarHandler() );
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<GoodsType> selectByPage(int page_num, int page_size) {
        String sql=" SELECT * FROM tb_goods_type  ORDER BY id LIMIT ?,? ";

        try {
            List<GoodsType> list= queryRunner.query(sql,new BeanListHandler<>(GoodsType.class),(page_num-1)*page_size, page_size);
             return  list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
